﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Meridian.ViewModel;
using VkApi;

namespace Meridian.Views
{
    /// <summary>
    /// Interaction logic for MiniView.xaml
    /// </summary>
    public partial class MiniView : Window
    {
        public event EventHandler Restore;

        public MiniView()
        {
            InitializeComponent();

            this.DataContext = App.ViewModel;

            AudioList.MouseDoubleClick += (s, e) =>
            {
                if (AudioList.SelectedIndex != -1)
                {
                    App.ViewModel.PlayAudio((VkAudio)AudioList.SelectedItem);
                }
            };

            if (App.ViewModel.IsPlayingFromSearch)
            {
                AudioList.SetBinding(ItemsControl.ItemsSourceProperty, "SearchResults");
                AudioList.SetBinding(Selector.SelectedIndexProperty, "SearchSelectedIndex");
            }

            App.ViewModel.PropertyChanged += ViewModelPropertyChanged;
            TopMostItem.IsChecked = App.Settings.MiniTopMost;
            AudioList.ScrollIntoView(App.ViewModel.CurrentAudio);
        }

        void ViewModelPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "IsPlaying":
                    if (App.ViewModel.IsPlaying)
                        ThumbPlayButton.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Meridian;component/Resources/Images/pause_thumb.png"));
                    else
                        ThumbPlayButton.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Meridian;component/Resources/Images/play_thumb.png"));
                    break;
                case "Shuffle":
                    if (App.ViewModel.Shuffle)
                        ThumbShuffleButton.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Meridian;component/Resources/Images/shuffle_icon_thumb.png"));
                    else
                        ThumbShuffleButton.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Meridian;component/Resources/Images/shuffle_disabled_icon_thumb.png"));
                    break;
            }
        }

        private void WindowMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && e.GetPosition(this).Y < 38)
            {
                DragMove();
                App.Settings.Left = this.Left;
                App.Settings.Top = this.Top;
            }
        }

        private void MiniModeButtonClick(object sender, RoutedEventArgs e)
        {
            App.Settings.MiniTopMost = TopMostItem.IsChecked;

            App.ViewModel.PropertyChanged -= ViewModelPropertyChanged;
            if (Restore != null)
                Restore(this, EventArgs.Empty);
        }

        private void CloseButtonClick(object sender, RoutedEventArgs e)
        {
            App.Settings.MiniTopMost = TopMostItem.IsChecked;
            this.Close();
        }

        private void MinimizeButtonClick(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void PrevButtonClick(object sender, RoutedEventArgs e)
        {
            App.ViewModel.PrevAudio();
        }

        private void PlayPauseButtonClick(object sender, RoutedEventArgs e)
        {
            App.ViewModel.PlayPause();
        }

        private void NextButtonClick(object sender, RoutedEventArgs e)
        {
            App.ViewModel.NextAudio();
        }

        private void MyAudioListScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            var sb = e.OriginalSource as ScrollViewer;
            if (sb == null)
                return;

            if (Math.Abs(e.VerticalOffset + e.ViewportHeight - e.ExtentHeight) < 5)
            {
                if (!App.ViewModel.IsPlayingFromSearch)
                    App.ViewModel.LoadMore(App.Settings.DynamicLoadItemsCount);
                else
                    App.ViewModel.SearchMore(App.Settings.DynamicLoadItemsCount);
            }
        }

        private void LikeButtonClick(object sender, RoutedEventArgs e)
        {
            App.ViewModel.LikeDislike();
        }

        private void ThumbShuffleButtonClick(object sender, EventArgs e)
        {
            App.ViewModel.Shuffle = !App.ViewModel.Shuffle;
        }

        private void ThumbPrevButtonClick(object sender, EventArgs e)
        {
            App.ViewModel.PrevAudio();
        }

        private void ThumbPlayButtonClick(object sender, EventArgs e)
        {
            App.ViewModel.PlayPause();
        }

        private void ThumbNextButtonClick(object sender, EventArgs e)
        {
            App.ViewModel.NextAudio();
        }
    }
}
